﻿
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using Model;
using MsDAL;
using System.ComponentModel;
using Components;

namespace ServiceHelper
{
    [DataObjectAttribute]
    public partial class Pot4a00Manager : Pot4a00Base
    {
        private Pot4a00DAL pot4a00Dal = new Pot4a00DAL();
        private static Pot4a00Manager _entity;

        public static Pot4a00Manager Entity
        {
            get
            {
                if (_entity == null)
                    _entity = new Pot4a00Manager();
                return _entity;
            }
        }

        #region 个人代码
        public string SaveData(Hashtable tb, DataTable dtl, DataTable deldata, string action, string usercode)
        {
            List<SqlApp> lst = new List<SqlApp>();

            Pot4a00 model = null;
            if (dtl == null || dtl.Rows.Count == 0 && "A" == action) return "明细中没有数据，不需要保存！";

            string appstr = Context.GetAppByUserCode(usercode);

            //使用该类的原因是为了生成连续的单号
            using (System.Transactions.TransactionScope scope = new System.Transactions.TransactionScope())
            {
                //单据号采用编码规则生成
                string promocode = ("A" == action) ? Sys0a60Manager.Entity.CreateSn("pot4a00_code", usercode) :
                    Components.Validation.ToString(tb["promocode"]); //促销号
                DateTime startdate = Convert.ToDateTime(tb["startdate"]); //促销开始日
                DateTime endate = Convert.ToDateTime(tb["endate"]); //促销结束日

                //新增或者修改的情况
                if ("X" != action)
                {
                    if ("M" == action) //先删除之
                    {
                        lst.Add(pot4a00Dal.DeleteByListid(promocode)); //删除明细表

                        lst.Add(pot4a00Dal.DeleteForUpdate(promocode)); //删除概要表
                    }

                    string pid;
                    double oprice;
                    double nprice;

                    if (null != dtl)
                    {
                        foreach (DataRow dr in dtl.Rows)
                        {
                            pid = Validation.ToString(dr["pid"]);
                            oprice = Validation.ToDouble(dr["oprice"]); //原出货价
                            nprice = Validation.ToDouble(dr["nprice"]);  //现出货价

                            model = new Pot4a00();
                            model.Nprice = nprice;
                            model.Startdate = startdate;
                            model.Endate = endate;
                            model.Pid = pid;
                            model.Oprice = oprice;
                            model.Muser = appstr;
                            model.Promocode = promocode;

                            model.Listid = Sys0a60Manager.Entity.CreateSn("pot4a00_listid", usercode);
                            lst.Add(this.Add(model));
                        }
                    }

                    //回写促销销售商门店表
                    string shoplist = Validation.ToString(tb["shoplist"]);
                    string[] shops = shoplist.Split(',');
                    string[] item;
                    Pot4a01 pot = null;
                    foreach (string sellerAndShop in shops)
                    {
                        item = sellerAndShop.Split(';');
                        if (item.Length < 2) return "门店格式错误，请重新操作！";

                        pot = new Pot4a01();
                        pot.Listid = Sys0a60Manager.Entity.CreateSn("pot4a01_listid", usercode);
                        pot.Plistid = promocode;
                        pot.Slistid = item[0];
                        pot.Splistid = item[1];

                        lst.Add(Pot4a01Manager.Entity.Add(pot));
                    }
                }
                else //仅修改结束时间
                {
                    //根据促销号更新促销结束时间
                    lst.Add(pot4a00Dal.UpdateEndDate(promocode, endate));
                }

                string msg = CommonManager.Entity.SaveData(lst);
                if (!string.IsNullOrEmpty(msg)) //是否提交事务
                    scope.Dispose();
                else
                    scope.Complete();

                return msg;
            }
        }

        #endregion
    }
}



